module.exports = function($scope, $modal, $resource, toaster){

    $scope.bigCurrentPage = 1;      //当前页码
	$scope.itemsPerPage = 10;       //每页显示几条

	//统计已申请的人数和金额
	$scope.countNum = function(){
		$resource('/api/as/puc/subsidycount/getCountForCheck', {}, {}).save({}, function(res){
            if(res.errcode === 0){
				$scope.count = res.data;
            } else {
                toaster.error({title: '', body: '统计失败'});
            }
        });
	}
	$scope.countNum();
	
	//查询已申请补贴列表
    $scope.load = function(pageNo){
        var para = {
            'pageNo': pageNo, 
			'pageSize': $scope.itemsPerPage
        };
        $resource('/api/ac/puc/subsidyCountService/findCheckCountList', {}, {}).save(para, function(res){
            if(res.errcode === 0){
				$scope.list = res.data.results;
				$scope.totalItems = res.data.totalRecord;
				$scope.bigCurrentPage = pageNo;
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
	};
	$scope.load(1);

	//审批
    $scope.checkInfo = function(obj){
		var modalInstance = $modal.open({
			template: require('../views/checkinfo.html'),
			controller: 'checkinfo',
			size: 'lg',
			resolve: {
				obj: function () {
					return obj;
				}
			}
		});
		modalInstance.result.then(function(result){
			$scope.load($scope.bigCurrentPage);
			$scope.countNum();
		});
    	
	};
	


	//导出核销明细
	$scope.exportDetail = function(obj) {
		var para = {
			'time': obj.time,
			'company_code': obj.company_code
		}
		$resource('/api/ac/puc/subsidyCountService/findExportInfoList', {}, {}).save(para, function(res){
            if(res.errcode === 0){
				var exportList = [];
				var actual_money = 0;
				angular.forEach(res.data, function(item, index){
					var temp = {
						'序号': index + 1,
						'产品名称': item.out_name,
						'使用时间': item.order_state_time,
						'被补贴人姓名': item.name,
						'联系方式': item.mobile,
						'证件号码': item.cardno,
						'补贴金额': item.subsidy_num / 100
					}
					exportList.push(temp);
					actual_money += item.subsidy_num;
				})
				exportList.push({
					'序号': '合计',
					'补贴金额': actual_money / 100
				});

				$scope.exportData(exportList, obj.time+'月份核销明细');
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
	}


	//导出数据
	$scope.exportData = function(data, title) {
		//这里的数据是用来定义导出的格式类型
        const wopts = { bookType: 'xlsx', bookSST: false, type: 'binary' };
        const wb = { SheetNames: ['Sheet1'], Sheets: {}, Props: {} };
        wb.Sheets['Sheet1'] = XLSX.utils.json_to_sheet(data);
        var blob = new Blob([str2binary(XLSX.write(wb, wopts))], {type: 'application/octet-stream' });

		var dlink = document.createElement("a");
        dlink.download = title + '.xlsx';
        dlink.href = URL.createObjectURL(blob);
        dlink.click();
        setTimeout(function () {
            URL.revokeObjectURL(blob); //释放这个object URL
        }, 100);
	}

	//导出excel前的二进制转码
    function str2binary(s) {
        if (typeof ArrayBuffer !== 'undefined') {
            var buf = new ArrayBuffer(s.length);
            var view = new Uint8Array(buf);
            for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
            return buf;
        } else {
            var buf = new Array(s.length);
            for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;
            return buf;
        }
    }
    
};